package com.windaka.elements.utils;


import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * <p><strong>权限相关工具类</strong></p>
 * <li>
 * <p>
 *     <strong>示例1: 获取当前用户的账号</strong> <br>
 *     String username = SecurityUtils.getUsername();
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例2: 获取当前用户的角色列表</strong> <br>
 *     List<String> authorityList = SecurityUtils.getUserAuthorities();<br>
 *     结果: authorityList: ["ROLE_ADMIN", "ROLE_USER" ...]
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例3: 判断用户是否有某个权限</strong> <br>
 *     boolean hasAdmin = SecurityUtils.hasAuthority("ROLE_ADMIN");<br>
 *     结果: hasAdmin: false
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例4: 获取用户的Token</strong> <br>
 *     String userToken = SecurityUtils.getUserToken();<br>
 *     结果: userToken: 282f50bb-8039-4912-bfb9-72dec8d81189
 * </p>
 * </li>
 * @author song
 *
 * @see SecurityContextHolder
 * @see OAuth2Authentication
 * @see SimpleGrantedAuthority
 * @since 15:09 2021/11/17
 **/
public class SecurityUtils {

    private SecurityUtils() {}

    public static final String ROLE_ADMIN = "ROLE_ADMIN";

    public static final String ROLE_PREFIX = "ROLE_";

    /**
     * <li>
     * <p>
     *     <strong>示例1: 获取当前用户的账号</strong> <br>
     *     String username = SecurityUtils.getUsername();
     * </p>
     * </li>
     * <li>
    **/
    public static String getUsername() {
        try {
            OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
            return authentication.getName();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * <li>
     * <p>
     *     <strong>示例2: 获取当前用户的角色列表</strong> <br>
     *     List<String> authorityList = SecurityUtils.getUserAuthorities();<br>
     *     结果: authorityList: ["ROLE_ADMIN", "ROLE_USER" ...]
     * </p>
     * </li>
     * <li>
    **/
    public static List<String> getUserAuthorities() {
        try {
            OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
            List<SimpleGrantedAuthority> authorityList =
                    (List<SimpleGrantedAuthority>) authentication.getUserAuthentication().getAuthorities();
            List<String> resultList = new ArrayList<>();
            for (SimpleGrantedAuthority sga : authorityList) {
                String role = sga.getAuthority();
                resultList.add(role);
            }
            return resultList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * <li>
     * <p>
     *     <strong>示例3: 判断用户是否有某个权限</strong> <br>
     *     boolean hasAdmin = SecurityUtils.hasAuthority("ROLE_ADMIN");<br>
     *     结果: hasAdmin: false
     * </p>
     * </li>
    **/
    public static Boolean hasAuthority(String authority) {
        // 判断是否为空 以及是否为 ROLE_ 开头
        if (CheckParamUtils.isNullOrEmpty(authority) || !authority.startsWith(ROLE_PREFIX)) {
            return false;
        }
        List<String> userAuthorityList = getUserAuthorities();
        for (String each : userAuthorityList) {
            if (CheckParamUtils.isNullOrEmpty(each)) {
                continue;
            }
            // 有权限 返回true
            if (each.equals(authority)) {
                return true;
            }
        }
        return false;
    }

    /**
     * <li>
     * <p>
     *     <strong>示例4: 获取当前用户的Token</strong> <br>
     *     String userToken = SecurityUtils.getUserToken();<br>
     *     结果: userToken: 282f50bb-8039-4912-bfb9-72dec8d81189
     * </p>
     * </li>
     **/
    public static String getUserToken() {
        OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        return details.getTokenValue();
    }
}
